package com.zsrt.java.day20170113.lh.daily;


import static org.junit.Assert.*;

import java.lang.reflect.Method;
import java.math.BigDecimal;

import org.junit.Test;

/**
 * 通过一个类名拿到所有东西
 * @author My
 *
 */
public class MethodTest {
	
	public static void main(String[] args) throws ClassNotFoundException {
		Class<?> clazz=Class.forName("com.zsrt.java.day20170113.lh.daily.Student");
		
		Method[] mothods = clazz.getDeclaredMethods();
		for (Method method : mothods) {
			System.out.println(method);
			
		}
	}
	
	@Test
	public void callMethod() throws Exception {
		
		Class<?> clazz=Class.forName("com.zsrt.java.day20170113.lh.daily.Student");
		
		//clazz代表一份字节码 ，obj代表的是一个对象的实例 
		Object obj = clazz.newInstance();
		
		Method method = clazz.getDeclaredMethod("add",int.class,int.class);
		System.out.println(method);
		
		Object invoke = method.invoke(obj,1,2);
		System.out.println(invoke);
		
		
		
	}
	
	@Test
	public void addBigDecimal() throws Exception {
		
		//由反射得到字节码
		Class<?> clazz=Class.forName("com.zsrt.java.day20170113.lh.daily.Student");
		
		//实例化
		Object student = clazz.newInstance();
		
		//拿到方法
		Method method=clazz.getDeclaredMethod("add",double.class,double.class);
		
		//打开权限
		method.setAccessible(true);
		
		//调用
		Object jg = method.invoke(student, 1.23,3.456);
		System.out.println(jg);
		
		
		
	}

}
